package com.whitedust.top50;

import com.whitedust.structure.TreeNode;

import java.util.*;

/**
 * @author A
 * @since 2025/11/7 12:56
 */

public class Top45 {
    public int[] maxSlidingWindow(int[] nums, int k) {
        Deque<Integer> queue = new LinkedList<>();
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            while (!queue.isEmpty() && queue.getFirst() < i - k + 1) queue.pollFirst();
            while (!queue.isEmpty() && nums[queue.getLast()] < nums[i]) queue.pollLast();
            queue.add(i);
            if (i - k + 1 >= 0)
                res.add(nums[queue.peek()]);
        }
        Integer[] array = res.toArray(new Integer[0]);
        return Arrays.stream(array).mapToInt(Integer::intValue).toArray();
    }
}
